<?php
    /* functions */
    function serviceBeme($info, $sms)
    {
        $url = "http://api2.phang.mobi/bem/?data=";    
        
        $reqid = round(microtime(true) * 10000);
        $info .=  "&reqid=" . $reqid . "&partner=NHATQUANG&test=1";
        
        $data_content = trim(urlencode(base64_encode($info)));
        $url .= $data_content;   
        
        $sms_content = urlencode(base64_encode($sms));
        $url .= "&sms=" . $sms_content;
        $response = cUrl($url);
        
        return $response;
    }
    
    function buildSyntaxDK($content, $regx)
    {
        preg_match($regx, $content, $match);
        $refCode = !empty($match[2]) ? $match[2] : "";
        $username = !empty($match[5]) ? $match[5] : "";
        $password = !empty($match[7]) ? $match[7] : "";
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["user_name"] = !empty($user) ? $user["username"] : "";
        $result["username"] = $username;
        $result["password"] = $password;
            
        return $result;          
    }
    
    function buildSyntaxDKInvite($content, $regx)
    {
        preg_match($regx, $content, $match);
        $refCode = !empty($match[2]) ? $match[2] : "";
        $username = !empty($match[5]) ? $match[5] : "";
        $password = !empty($match[7]) ? $match[7] : "";
        $username_invite = !empty($match[9]) ? $match[9] : "";
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["user_name"] = !empty($user) ? $user["username"] : "";
        $result["username"] = $username;
        $result["password"] = $password;
        $result["username_invite"] = $username_invite;
            
        return $result;
    }
    
    function buildSyntaxNAP($content, $regx)
    {
        preg_match($regx, $content, $match);
        $refCode = !empty($match[2]) ? $match[2] : "";
        $username = !empty($match[5]) ? $match[5] : "";        
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["username"] = $username;        
            
        return $result;
    }
    
    function buildSyntaxRESET($content, $regx)
    {
        preg_match($regx, $content, $match);
        $refCode = !empty($match[2]) ? $match[2] : "";
        $username = !empty($match[5]) ? $match[5] : "";        
        
        $user = getUser($refCode);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["username"] = $username;        
            
        return $result;          
    } 
    
    /*
    * processing
    */
    
    function processing($data)
    {
        $regx_register = "/^TVB(\s+)NHATQUANG([a-zA-Z0-9_]{1,})(\s+)DK(\s+)([a-zA-Z0-9_.]{1,})(\s+)([a-zA-Z0-9_.]{1,})$/si";
        $regx_register_invite = "/^TVB(\s+)NHATQUANG([a-zA-Z0-9_]{1,})(\s+)DK(\s+)([a-zA-Z0-9_.]{1,})(\s+)([a-zA-Z0-9_.]{1,})(\s+)([a-zA-Z0-9_.]{1,})$/si";        
        $regx_nap = "/^TVB(\s+)NHATQUANG([a-zA-Z0-9_]{1,})(\s+)NAP(\s+)([a-zA-Z0-9_.]{1,})$/si";
        $regx_reset = "/^TVB(\s+)NHATQUANG([a-zA-Z0-9_]{1,})(\s+)RESET(\s+)([a-zA-Z0-9_.]{1,})$/si";        
        
        $data_reg = array();
        if(
            preg_match($regx_register, $data["content"]) 
            || preg_match($regx_register_invite, $data["content"]) 
            || preg_match($regx_reset, $data["content"]) 
            || preg_match($regx_nap, $data["content"])
        ){
            
            if(preg_match($regx_register, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntaxDK($data["content"], $regx_register);
                $list_service_code = array("6522");
                $info = "sender=" . $data["sender"] . "&recipient=" . $data["serviceCode"] . "&service=DK&message=" . $syntax["username"] . " " . $syntax["password"];
                $data["action"] = "reg"; //action
                
                $data_reg["game_user"] = $syntax["username"]; 
                $data_reg["username"] = $syntax["user_name"]; 
                
            } else if(preg_match($regx_register_invite, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntaxDKInvite($data["content"], $regx_register_invite);
                $list_service_code = array("6522");
                $info = "sender=" . $data["sender"] . "&recipient=" . $data["serviceCode"] . "&service=DK&message=" . $syntax["username"] . " " . $syntax["password"] . " " . $syntax["username_invite"];
                $data["action"] = "reg"; //action
                
                $data_reg["game_user"] = $syntax["username"];
                $data_reg["username"] = $syntax["user_name"];
                
            } else if(preg_match($regx_reset, $data["content"])){
                
                /* nạp lượng */
                $syntax = buildSyntaxRESET($data["content"], $regx_reset);
                $list_service_code = array("6522", "6622", "6722");
                $info = "sender=" . $data["sender"] . "&recipient=" . $data["serviceCode"] . "&service=RESET&message=" . $syntax["username"];
                $data["action"] = "reset"; //action
                
            } else if(preg_match($regx_nap, $data["content"])){
                
                /* nạp xu */
                $syntax = buildSyntaxNAP($data["content"], $regx_nap);
                $list_service_code = array("6522", "6622", "6722");
                $info = "sender=" . $data["sender"] . "&recipient=" . $data["serviceCode"] . "&service=NAP&message=" . $syntax["username"];                
                $data["action"] = "nap"; //action
                
            }
            
            $data["userId"] = $syntax["userId"]; 
            if(in_array($data["serviceCode"], $list_service_code)){                            
                $refBeme = serviceBeme($info, $data["content"]);                        
                
                if(preg_match($regx_register, $data["content"])){
                    if($refBeme=="0"){
                        $response["status"] = 1;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Ban da dang ky thanh cong.\nTai khoan dang nhap cua ban la:\nUser: " . $syntax["username"] . "\nPassword: " . $syntax["password"];
                        
                        $data["status"] = 1;
                    } else if($refBeme=="4") {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Tai khoan ban dang ky da ton tai. Xin vui long chon tai khoan dang nhap khac";
                        
                        $data["status"] = 0;
                    } else {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Dang ky tai khoan khong thanh cong";   
                        
                        $data["status"] = 0;
                    }    
                } else if(preg_match($regx_register_invite, $data["content"])){
                
                    if($refBeme=="0"){
                        $response["status"] = 1;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Ban da dang ky thanh cong.\nTai khoan dang nhap cua ban la:\nUser: " . $syntax["username"] . "\nPassword: " . $syntax["password"] . "\nSo KOIN dc tang: 10.000";
                        
                        $data["status"] = 1;
                    } else if($refBeme=="4") {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Tai khoan ban dang ky da ton tai. Xin vui long chon tai khoan dang nhap khac";
                        
                        $data["status"] = 0;
                    } else {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Dang ky tai khoan khong thanh cong";   
                        
                        $data["status"] = 0;
                    }   
                    
                } else if(preg_match($regx_nap, $data["content"])){
                    
                    if($refBeme=="0"){
                        $response["status"] = 1;
                        $response["res_status"] = $refBeme;
                        if($data["serviceCode"]=="6522"){
                            $response["msg"] = "Ban da nap thanh cong 25.000 Koin vao tai khoan " . $syntax["username"] . "\nThoat game va vao lai de cap nhat KOIN";    
                        } else if($data["serviceCode"]=="6622"){
                            $response["msg"] = "Ban da nap thanh cong 15.000 Koin vao tai khoan " . $syntax["username"] . "\nThoat game va vao lai de cap nhat KOIN";
                        } else if($data["serviceCode"]=="6722"){
                            $response["msg"] = "Ban da nap thanh cong 5.000 Koin vao tai khoan " . $syntax["username"] . "\nThoat game va vao lai de cap nhat KOIN";
                        } 
                        
                        $data["status"] = 1;                   
                    } else if($refBeme=="7") {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Khong tim thay tai khoan can nap tien trong he thong";
                        
                        $data["status"] = 0;
                    } else if($refBeme=="9") {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Mot so dien thoai khong the nap Koin qua 10 lan trong 1 ngay";
                        
                        $data["status"] = 0;
                    } else {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        $response["msg"] = "Nap tien khong thanh cong";   
                        
                        $data["status"] = 0;
                    }   
                    
                } else if(preg_match($regx_reset, $data["content"])){
                    
                    if(preg_match("/0[|][a-zA-Z0-9]{1,}$/si", $refBeme)){
                        $refBeme = explode("|", $refBeme);
                        $response["status"] = 1;
                        $response["res_status"] = $refBeme[0];
                        $response["msg"] = "Lay mat khau cua tai khoan " . $syntax["username"] . " thanh cong \nMat khau moi cua ban la " . $refBeme[1];    
                        
                        $data["status"] = 1;
                    } else {
                        $response["status"] = 2;
                        $response["res_status"] = $refBeme;
                        if($refBeme=="10") {
                            $response["msg"] = "SDT nay khong the doi password cua tai khoan " . $syntax["username"] . ". Phai dung SDT khi dang ky tai khoan de doi mat khau";
                        } else {                        
                            $response["msg"] = "Lay lai mat khau that bai";   
                        }    
                        
                        $data["status"] = 0;
                    }   
                    
                }
                                
            } else {
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;        
            }               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
        }
        
        /* log sms */
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 52; //ID partner mvcorp    
        $data["type"] = 1; //Thể loại game   
        $data["id"] = 144; //ID game beme        
        $data["output"] = $response["status"] . "|" . $response["res_status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
         
        $result = InsertSmsLog($data);   
        
        if($data["action"]=="reg"){
            $data_reg["userId"] = $data["userId"];    
            $data_reg["gameId"] = $data["id"];    
            
            /* Check xem user da ton tai chua */
            $result = CheckGameUser($data_reg);
            if($result==0){
                $result = InsertGameUser($data_reg);    
            }
        }
        
        return $response;
    }
?>